package com.haoshuang.zhouzhoubang.ui.page.mine.wallet.frozen.record

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityFrozenAmountRecordBinding
import com.haoshuang.zhouzhoubang.model.bean.DepositGroupByDayBean
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.one.RecognizancePayOneActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.wallet.frozen.detail.FrozenAmountDetailActivity
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import java.math.BigDecimal

/**
 *  冻结金额记录
 */
class FrozenAmountRecordActivity :
    BaseVMActivity<FrozenAmountRecordViewModel, ActivityFrozenAmountRecordBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<FrozenAmountRecordActivity>()
        }
    }

    var list = arrayListOf<DepositGroupByDayBean.DepositGroupBean>()
    lateinit var adapter: ForzenDayGroupAdapter
    var fcate_id = ""
    var service_id = ""
    var is_deposit = "0"
    var is_team = 0
    var deposit_tip = ""
    var ids = 0
    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "冻结金额"
        binding.titleBar.leftImageView().click { finish() }

        fcate_id = intent.getStringExtra("fcate_id").toString()
        service_id = intent.getStringExtra("service_id").toString()
        is_team = intent.getIntExtra("is_team", 0)
        ids = intent.getIntExtra("ids", 0)
        val price = intent.getStringExtra("price").toString()
        val bd = BigDecimal(price)
        val value = bd.setScale(2, BigDecimal.ROUND_HALF_UP)

        binding.tvPrice.text = value.toString()

        adapter = ForzenDayGroupAdapter(list) { item, position ->
            startPage<FrozenAmountDetailActivity>(
                "ids" to item?.id,
                "money" to item?.money,
                "is_deposit" to is_deposit,
                "is_team" to is_team,
                "deposit_tip" to deposit_tip
            )
        }

        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = adapter
        binding.tvNext.clickWithTrigger {
            XPopup.Builder(mActivity)
                .asCustom(ConfirmDialog(
                    context = mActivity,
                    title = "解冻说明",
                    content = deposit_tip,
                    cancelStr = "我再等等",
                    confirmStr = "去缴纳",
                    isHideCancel = false,
                    onCancel = {}) {

                    // 0未缴纳1补缴
                    startPage<RecognizancePayOneActivity>("team_id" to ids, "service_id" to fcate_id)

                    finish()
                })
                .show()
        }

    }

    override fun initData() {

        vm.getMasterDepositGroupByDayList(fcate_id, is_team)

    }

    override fun startObserve() {
        vm.getMasterDepositGroupByDayListData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list.clear()
                list.addAll(it?.data?.list?.toMutableList() ?: mutableListOf())
                is_deposit = it?.data?.is_deposit.toString()
                deposit_tip = it?.data?.deposit_tip.toString()
                adapter.notifyDataSetChanged()
            }
        }
    }

}